package month5.day8;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("47.105.174.110", 8080);
            //读服务器信息
            new ClientReceive(socket);
            //发送信息
            new ClientSend(socket);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    //向服务器发送信息
    public static class ClientSend implements Runnable {
        Scanner in = new Scanner(System.in);
        private Socket socket;

        public ClientSend(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                PrintStream ps = new PrintStream(socket.getOutputStream());
                String line;
                while ((line = in.nextLine()) != null) {
                    ps.println(line);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    //接受服务器信息
    public static class ClientReceive implements Runnable {
        private Socket socket;

        public ClientReceive(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
